<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" @click="addVisible = true" size="mini">添加分类</el-button>
      </div>
       <el-table
       stripe
        border
      :data="artCateList"
      style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="分类名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="cate_alias"
        label="分类别名">
      </el-table-column>
      <el-table-column
        label="操作">
        <template v-slot='scope'>
        <el-button type="primary"  @click="editArtCate(scope.row)" size="mini">修改</el-button>
        <el-button type="danger"  @click="doDel(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>
     </el-table>
    </el-card>

    <!-- 添加分类 -->
    <el-dialog
    @closed="$refs.addForm.resetFields()"
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%">
      <span>
        <el-form ref="addForm" :rules="artCateRules" :model="addForm" label-width="70px">
         <el-form-item prop='cate_name' label="分类名称">
             <el-input v-model="addForm.cate_name"></el-input>
         </el-form-item>
         <el-form-item prop='cate_alias' label="分类别名">
             <el-input v-model="addForm.cate_alias" ></el-input>
         </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="doAdd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改分类 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="editVisible"
      width="35%">
      <span>
        <el-form ref="editForm" :rules="artCateRules" :model="editForm" label-width="70px">
         <el-form-item prop='cate_name' label="分类名称">
             <el-input v-model="editForm.cate_name"></el-input>
         </el-form-item>
         <el-form-item prop='cate_alias' label="分类别名">
             <el-input v-model="editForm.cate_alias" ></el-input>
         </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="doedit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      artCateList: [],
      //   设置 dialog 是否显示
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {},
      artCateRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '必须为1-10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '必须为1-15位数字或字母', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      //   console.log(res)
      this.artCateList = res.data
    },
    doAdd () {
      // 兜底
      this.$refs.addForm.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addVisible = false
        this.getCateList()
      })
    },
    editArtCate (row) {
    //   console.log(row)
      this.editForm = { ...row }
      this.editVisible = true
    },
    doedit () {
      // 兜底
      this.$refs.editForm.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.editVisible = false
        this.getCateList()
      })
    },
    doDel (id) {
      this.$confirm('是否确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        //   console.log(res)
        if (res.code !== 0) return
        this.getCateList()
      }).catch(() => {})
    }
  },
  created () {
    this.getCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
